package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Date;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper clueTrackRecordMapper;

    /**
     * 根据 clueId 查询线索跟进记录列表功能
     * @param clueId
     * @return
     */
    @Override
    public TableDataInfo findAll(Long clueId) {
        // 1. 调用方法根据 clueId 获取跟进记录类 TbClueTrackRecord 集合
        List<TbClueTrackRecord> clueTrackRecordList = clueTrackRecordMapper.selectAllByClueId(clueId);

        // 2. 调用方法根据 clueId 获取跟进记录的总数量
        Integer count = clueTrackRecordMapper.selectCountBuClueId(clueId);

        // 3. 封装返回类
        TableDataInfo tableDataInfo = new TableDataInfo(clueTrackRecordList, count);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");

        // 4. 返回结果数据
        return tableDataInfo;
    }

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;
    @Override
    public String add(ClueTrackRecordVo tbClueTrackRecord) {
        //创建TbClueTrackRecord 对象
        TbClueTrackRecord record = new TbClueTrackRecord();
        BeanUtils.copyProperties(tbClueTrackRecord,record);
        //获取当前操作人的名字
        String username = SecurityUtils.getUsername();
        record.setCreateTime(new Date());
        //更新线索表的数据 线索表
        tbClueMapper.update(tbClueTrackRecord);
        //新增线索跟进记录表
        tbClueTrackRecordMapper.add(record,username);
        return "操作成功";
    }
}
